package com.example.controller;

import com.example.common.BaseResponse;
import com.example.common.ErrorCode;
import com.example.common.ResultUtils;
import com.example.exception.BusinessException;
import com.example.model.PageBean;
import com.example.model.user.User;
import com.example.service.LogService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.example.contant.UserConstant.SUPER_ADMIN_ROLE;
import static com.example.contant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/logs")
public class LogController {
    @Resource
    private LogService logService;

    /**
     * 获取日志信息
     * @param pageSize 分页尺寸
     * @param pageNum 页数
     * @param request HttpServletRequest
     * @return 分页查询结果
     */
    @GetMapping()
    public BaseResponse<PageBean> getLogs(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "15")Integer pageSize,
                                          HttpServletRequest request) {
        Object userObj = request.getSession().getAttribute(USER_LOGIN_STATE);
        User user = (User) userObj;
        if (user == null || user.getUserRole() != SUPER_ADMIN_ROLE)
            throw new BusinessException(ErrorCode.NO_AUTH);

        PageBean pageBean=logService.getLogs(pageNum,pageSize);
        return ResultUtils.success(pageBean);
    }
}
